package com.API.beans;

import com.API.dal.entities.*;
import com.API.dal.providers.*;
import org.primefaces.component.tree.Tree;
import org.primefaces.context.RequestContext;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.LinkedList;
import java.util.List;

/**
 * Каласс будет управлять страницей просмотра информации по проекту
 * разделение в следствии снижения нагрузки на классы
 */

@Component
@Scope("request")
public class ProjectMainBean {

    @Autowired
    ProjectProvider projectProvider;
    @Autowired
    EmployeesProvider employeesProvider;
    @Autowired
    SalaryProvider salaryProvider;
    @Autowired
    CostsProvider costsProvider;
    @Autowired
    EquipmentsProvider equipmentsProvider;

    TreeNode projectProperty;
    TreeNode projectTree;
    TreeNode selectedTree;
    Objects selectedProject;
    TreeNode property;




    public TreeNode getSelectedTree() {
        return selectedTree;
    }

    public void setSelectedTree(TreeNode selectedTree) {
        this.selectedTree = selectedTree;
        String str = (String) selectedTree.getData();
        selectedProject = projectProvider.getProjectByName(str);
    }

    public ProjectMainBean(){
        projectProperty = new DefaultTreeNode("root",null);
        projectTree = new DefaultTreeNode("root1",null);

    }

    @PostConstruct
    public void init(){
        TreeNode[] project;

        List<Objects> projects = projectProvider.getProjects();
        project = new TreeNode[projects.size()];
        //  List<TreeNode> tree = new LinkedList<TreeNode>();
        for (int i = 0; i < projects.size(); i++){
            String str = projects.get(i).getName();
            project[i] = new DefaultTreeNode(str,projectTree);
            // tree.add(new DefaultTreeNode(str,projectTree));
        }
        if (projects.size() > 0)
            selectedProject = projects.get(0);
        else
            selectedProject = new Objects();

        /**  для дадьнейшего развития
         TreeNode empl = new DefaultTreeNode("emp","Сотрудники",projectProperty);
         TreeNode tech = new DefaultTreeNode("tech","Техника",projectProperty);
         TreeNode inc = new DefaultTreeNode("inc", "Поступления", projectProperty);
         TreeNode cost = new DefaultTreeNode("cost", "Затраты", projectProperty);

         **/

        TreeNode empl = new DefaultTreeNode("Сотрудники",projectProperty);
        TreeNode tech = new DefaultTreeNode("Техника",projectProperty);
        TreeNode inc = new DefaultTreeNode( "Поступления", projectProperty);
        TreeNode cost = new DefaultTreeNode("Затраты", projectProperty);

    }


    public Objects getSelectedProject() {
        return selectedProject;
    }

    public void setSelectedProject(Objects selectedProject) {
        this.selectedProject = selectedProject;
    }

    public TreeNode getProjectTree(){

        return  projectTree;
    }

    public TreeNode getProjectProperty(){

        return projectProperty;
    }

    public double getTotalCosts() {
        double total = 0;
        if (selectedProject.getId() != null){
        List<Costs> costList = costsProvider.getProjectCosts(selectedProject.getId());
        for (Costs cs : costList)
            total = total + cs.getTotal().doubleValue();
        }
        return total;
    }

    public double getTotalDept() {
        double totalDept = 0;
        if (selectedProject.getId() != null){
        List<EmployeeProject> costList = employeesProvider.getEmployeesProjects(selectedProject.getId());
        for (EmployeeProject ep : costList){
            totalDept += ep.getId().getTotal().doubleValue();
        }
        }
        return totalDept;
    }

    public void addMaterialCosts(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("addMaterialCostsDialog.show()");
    }


    public List<Employees> getEmployee(){
        List<Employees> employees = new LinkedList<Employees>();
        if (selectedProject.getId() != null){
            employees = employeesProvider.getProjectEmployees(selectedProject.getId());
            int id;
            for (Employees emp: employees){
                id = emp.getSalaryType().getId();
                emp.setSalaryType(salaryProvider.getSalaryTypeById(id));
            }
        }

        return  employees;

    }

    public void addMechanismCosts(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("addMechanismCostsDialog.show()");
    }

    public void addSomeCost(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("addSomeCostsDialog.show()");
    }

    public void addLabor(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("addProjectLaborDialog.show()");
    }

    public void addProject(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("addProjectDialog.show()");
    }


    public List<Costs> getProjectCosts(){
        List<Costs> costsList = new LinkedList<Costs>();
        if (selectedProject.getId() != null){
            costsList = costsProvider.getProjectCosts(selectedProject.getId());
            for (Costs ct: costsList){
                ct.setCostsTypes(costsProvider.getCostsTypesById(ct.getCostsTypes().getId()));
            }
        }
        return costsList;
    }


    public List<Incomes> getProjectIncomes(){
        return new LinkedList<Incomes>();
    }


    public List<Equipments> getMaterials() {
        List<Equipments> materials = new LinkedList<Equipments>();
        if (selectedProject.getId() != null)
            materials = projectProvider.getProjectMaterials(selectedProject.getId());
        return materials;
    }

    public List<Equipments> getMechanisms() {
        List<Equipments> mechanisms = new LinkedList<Equipments>();
        if (selectedProject.getId() != null)
            mechanisms = projectProvider.getProjectMechanisms(selectedProject.getId());
        return mechanisms;
    }

    public String getMaterialQuantity(int material_id){
        List<EquipmentsProjects> epl = projectProvider.getEquipmentTotal(selectedProject.getId(), material_id);
        double sum = 0;
        for (EquipmentsProjects ep: epl){
            sum += ep.getId().getTotal();
        }
        return "" + sum;
    }

    public String getMaterialTotal(int material_id){
        List<EquipmentsProjects> epl = projectProvider.getEquipmentTotal(selectedProject.getId(), material_id);
        double sum = 0;
        for (EquipmentsProjects ep: epl){
            sum += ep.getId().getTotalSum().doubleValue();
        }
        return "" + sum;
    }

    public String getMechanismHours(int mechanism_id){
        List<EquipmentsProjects> epl = projectProvider.getEquipmentTotal(selectedProject.getId(), mechanism_id);
        double sum = 0;
        for (EquipmentsProjects ep: epl){
            sum += ep.getId().getTotal();
        }
        return "" + sum;
    }

    public String getMechanismCosts(int mechanism_id){
        List<EquipmentsProjects> epl = projectProvider.getEquipmentTotal(selectedProject.getId(), mechanism_id);
        double sum = 0;
        for (EquipmentsProjects ep: epl){
            sum += ep.getId().getTotalSum().doubleValue();
        }
        return "" + sum;
    }

    public String getEmployeeDept(String name){
        List<EmployeeProject> depList = employeesProvider.getEmployeeProjects(name);
        double sum = 0;
        double val;
        for (EmployeeProject ep: depList){
               val = ep.getId().getTotal().doubleValue();
               sum += val;
        }
        return String.valueOf(sum);
    }

    public String getEmployeeDeptByProject(String name){
        List<EmployeeProject> depList = employeesProvider.getEmployeeProjects(name);
        double sum = 0;
        double val;
        for (EmployeeProject ep: depList){
            if (ep.getId().getProjectId() == selectedProject.getId()){
            val = ep.getId().getTotal().doubleValue();
            sum += val;
            }
        }
        return String.valueOf(sum);

    }


    public String getEmployeeHours(int employee_id){
        List<EmployeeProject> costsList = employeesProvider.getEmployeeHous(employee_id);
        double sum = 0;
        double val = 0;
        for (EmployeeProject cs: costsList){
           if (cs.getId().getProjectId() == selectedProject.getId())
               val = cs.getId().getQuantity();
                sum += val;
        }
        return String.valueOf(sum);

    }


}
